<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 1. 实现树形结构, pid就是每个成员的父级
      let data = [
        { id: 1, pid: 0 },
        { id: 2, pid: 1 },
        { id: 3, pid: 0 },
        { id: 4, pid: 2 },
      ];
      function Tree(data,pid=0){
        const pitem=data.filter(el => el.pid === pid);
        if(!pitem || pitem.length<=0){
          return null
        }
        pitem.forEach((e)=>{
          const res = Tree(data,e.id);
          if(res && res.length > 0){
            e.children = res
          }
        });
        return pitem
      }
      console.log(Tree(data));
      

      // 2. 不修改代码的情况下, 实现修改 obj 的内容
      const test = (() => {
        let obj = {
          a: '变量a',
          b: '变量b',
        };
        return {
          get: (k) => {
            return obj[k];
          },
        };
      })();
      // 网上查的，给对象原型上添加属性ab c，然后返回当前对象
      Object.defineProperty(Object.prototype, 'abc',{
        get(){
          return this 
        },
      })
      const newobj = test.get('abc');
      newobj.a= 'jjj';
      newobj.b= 'sldjf'
      newobj.c='变量c'
      console.log(test.get('a'));
      console.log(test.get('b'));
      console.log(test.get('c'));

      // 3. 得出以下结果, 并且说明原因
      window.number = 2;  
      let obj = {
        number: 3,
        db: (function () {
          this.number *= 4;
          return function () {
            this.number *= 5;
          };
        })(),
      };
      let db = obj.db;// 立即函数声明了，就会立即执行 ，this指向window ,window.number = 8
      db(); //函数调用，this指向window，window.number = 40
      obj.db(); //obj调用了函数，this指向obj，obj.number = 15
      // this
      console.log(window.number);//40
      console.log(obj.number);//15
    </script>
  </body>
</html>
